﻿using System;
using System.Linq;
using System.Drawing;
using System.Windows.Forms;
using Newtonsoft.Json.Linq;
using XFControls.Utils;
using XFSenseUI;
using XFControls.XFPanels;
using XFSenseUI.XFItems;
using FBEntities.FBObjects;
using XFControls.XFPanels.XFPanelItems;
using System.IO;
using System.Threading;
using XFControls;

namespace XDAFacebook
{
    public partial class PhotoUploader : Form
    {
        private bool uploading;

        public PhotoUploader()
        {
            Cursor.Current = Cursors.WaitCursor;
            Application.DoEvents();

            InitializeComponent();

            var list = new SenseListPanel { ScrollbarColor = Color.Gray };
            list.Attributes |= PanelAttributes.ScrollPast;

            xfPanelContainer.SetHeader(senseHeader);
            xfPanelContainer.SetMainPanel(list);

            senseHeader.CenterText = Settings.LocalizationStrings.uploadPhotosHeader;

            var image = new SenseItemFileBrowser
            {
                MainText = Settings.LocalizationStrings.choosePicture,
                SecondaryText = Settings.LocalizationStrings.noPicture,
                Filter = Settings.pictureFileExtensions
            };

            var upload = new SenseItem
            {
                MainText = Settings.LocalizationStrings.uploadPictureButton
            };

            var percCompSlider = new SenseItemSlider
            {
                MainText = Settings.LocalizationStrings.percentImageUpload,
                Percent = 0,
                ItemType = XFItemType.Display
            };

            var display = new XFItemImageDisplay();

            image.Click += (o, e) =>
                {
                    if (File.Exists(image.SecondaryText))
                    {
                        display.LoadImage(image.SecondaryText);
                    }
                };

            var caption = new SenseItemTextBox { Description = "Photo Caption:", MultiLine = true };
            caption.SetHeight(100);

            var albums = new SenseItemDropDown();
            if (!XFItemControlUtils.Me.Me.Albums.Parsed)
            {
                using (var wraper = XFItemControlUtils.Me.Me.Albums.Parse(XFItemControlUtils.Me.Me.id))
                {
                    var data = wraper.Download();
                    XFItemControlUtils.Me.Me.Albums.Parse(JObject.Parse(data));
                }
            }

            foreach (var item in XFItemControlUtils.Me.Me.Albums.data)
                albums.Options.Add(item.name);

            upload.Click += (o, e) =>
                {
                    if (image.SecondaryText != Settings.LocalizationStrings.noPicture)
                    {
                        if (uploading)
                        {
                            MessageBox.Show(Settings.LocalizationStrings.fileUploading);
                            return;
                        }

                        uploading = true;
                        Cursor.Current = Cursors.WaitCursor;
                        Application.DoEvents();

                        EventHandler<PercentCompleteArgs> perc = (percSender, percComp) =>
                        {
                            percCompSlider.Percent = percComp.Percent;
                        };

                        WaitCallback cb = v =>
                                          {
                                              //Thread.CurrentThread.Priority = ThreadPriority.BelowNormal;

                                              string album = "me";
                                              if (!string.IsNullOrEmpty(albums.Selected))
                                                  album = XFItemControlUtils.Me.Me.Albums.data.First(a => a.name == albums.Selected).id;

                                              var ret = FBParseUtilities.HasError(FBParseUtilities.PostPictureGraph(album, image.SecondaryText, caption.Text, perc));
                                              if (!IsDisposed)
                                                  Invoke(new Action(() =>
                                                                         {
                                                                             if (ret)
                                                                                 MessageBox.Show(Settings.LocalizationStrings.photoUploadError);
                                                                             else
                                                                             {
                                                                                 MessageBox.Show(Settings.LocalizationStrings.photoUploadSuccess);

                                                                                 percCompSlider.Percent = 0;
                                                                                 display.ClearImage();
                                                                                 list.Invalidate();
                                                                             }
                                                                         }));

                                              uploading = false;
                                          };

                        ThreadPool.QueueUserWorkItem(cb);

                        Cursor.Current = Cursors.Default;
                        Application.DoEvents();

                    }
                    else
                        MessageBox.Show(Settings.LocalizationStrings.pickAFile);
                };

            display.ImageClosed += (o, e) =>
                {
                    image.SecondaryText = Settings.LocalizationStrings.noPicture;
                    image.Invalidate();
                };

            list.Add(image);
            list.Add(percCompSlider);
            list.Add(display);
            list.Add(albums);
            list.Add(caption);
            list.Add(upload);

            Cursor.Current = Cursors.Default;
            Application.DoEvents();
        }

        private void menuItem1_Click(object sender, EventArgs e)
        {
            Close();
        }
    }
}